package downloader

import (
	"aqielife.cn/spider/config"
	"aqielife.cn/spider/log"
	"aqielife.cn/spider/models"
	"net/http"
)

var logger = log.NewSimpleLogger() // 日志记录器

type GenDownloader interface {
	Download(req *models.Request) *models.Response
}

type Downloader struct {
	client *http.Client
}

func NewDownloader() GenDownloader {
	return &Downloader{&http.Client{}}
}

func (d *Downloader) Download(req *models.Request) *models.Response {
	for k, v := range config.EastmoneyConfig.HttpHead {
		req.GetReq().Header.Set(k, v)
	}
	httpRes, err := d.client.Do(req.GetReq())
	if err != nil {
		logger.Infoln("DownloadError", err)
		return nil
	}
	return models.NewResponse(httpRes, req.GetIndex())
}
